﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class _13 : MonoBehaviour
{
    //为类型输出格式化字符串
    // Start is called before the first frame update
    void Start()
    {
        Person person = new Person() { IDCode = "公众号游戏一世", FirstName = "Zhang", LastName = "San" };
        Debug.Log(person.ToString());
        Debug.Log(person.ToString("ch",null));
        Debug.Log(person.ToString("en", null));

        PersongFormatter persongFormatter = new PersongFormatter();
        Debug.Log("----分割线----");

        Debug.Log(persongFormatter.Format("ch",person,null));
        Debug.Log(persongFormatter.Format("chm",person,null));
        Debug.Log(persongFormatter.Format("en",person,null));
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

public class PersongFormatter : IFormatProvider, ICustomFormatter
{
    public string Format(string format, object arg, IFormatProvider formatProvider)
    {
        Person person = arg as Person;
        if (person==null)
        {
            return string.Empty;
        }
        switch (format)
        {
            case "ch":
                return string.Format("{0} {1}", person.LastName, person.FirstName);
            case "en":
                return string.Format("{0} {1}", person.FirstName, person.LastName);
            case "chm":
                return string.Format("{0} {1} : {2}", person.LastName, person.FirstName,person.IDCode);
            default:
                return string.Format("{0} {1}", person.FirstName, person.LastName);
        }
    }

    public object GetFormat(Type formatType)
    {
        if (formatType == typeof(ICustomFormatter))
        {
            return this;
        }
        else
        {
            return null;
        }
    }
}

public class Person : IFormattable
{
    public string IDCode { set; get; }
    public string FirstName { set; get; }
    public string LastName { set; get; }

    public string ToString(string format, IFormatProvider formatProvider)
    {
        switch (format)
        {
            case "ch":
                return this.ToString();
            case "en":
                return string.Format("{0} {1}", FirstName, LastName);
            default:
                return this.ToString();
        }
    }

    public override string ToString()
    {
        return string.Format("{0} {1}", LastName, FirstName);
    }
}
